﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiXuLong.Model;
namespace ShiXuLong.Service
{
   
      public class RecommendService
    {
        ShoesEntities db = new ShoesEntities();
        /// <summary>
        /// 显示数据
        /// </summary>
        /// <returns></returns>
        public List<RecPositionModel> GetShow()
        {
            var shop = from item in db.RecPositions.Where(p => true)
                       select new RecPositionModel
                       {
                           Id = item.Id,
                          Site=item.Site

                       };
            return shop.ToList();
        }
        /// <summary>
        /// 通过id差数据显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ShopModel Edit(int id)
        {
            var shop = from item in db.Shops.Where(p => p.Id == id)
                       select new ShopModel
                       {
                           Id = item.Id,
                           ShopId = item.ShopId,
                           ShopName = item.ShopName,
                           ProductProfile = item.ProductProfile,
                           ShopTypeId = item.ShopTypeId,
                           RecommendId = item.RecommendId,
                           Display = item.Display,
                           Shopimage = item.Shopimage,
                           Price = item.Price,
                           Number = item.Number,
                           GuideId = item.GuideId,
                           Status = item.Status
                       };
            return shop.SingleOrDefault();
        }
        public RecommendModel EditRecommend(int id)
        {
            var rec = from item in db.Recommends.Where(p => p.Id == id)
                      select new RecommendModel
                      {
                          Id = item.Id,
                          RecId = item.RecId,
                          ShopId = item.ShopId,
                          Weight = item.Weight
                      };
            return rec.SingleOrDefault();
        }
        public int EditRecommendInfo(Recommend rec)
        {
            db.Entry(rec).State = System.Data.Entity.EntityState.Modified;
            return db.SaveChanges();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="emp"></param>
        /// <returns></returns>
        public int AddInfo(Shop emp)
        {
            db.Shops.Add(emp);
            int n = db.SaveChanges();
            return n;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="shop"></param>
        /// <returns></returns>
        public int EditInfo(Shop shop)
        {
            db.Entry(shop).State = System.Data.Entity.EntityState.Modified;
            return db.SaveChanges();
        }
        public int GetAddReccommend(Recommend recommend)
        {
            db.Recommends.Add(recommend);
            int n = db.SaveChanges();
            return n;
        }
        public List<RecommendModel> GetRecommed(int limit, int page, out int count)
        {
            var recommend = from item in db.Recommends.Where(p => true)
                            select new RecommendModel
                            {
                                Id = item.Id,
                                RecId = item.RecId,
                                ShopId = item.ShopId,
                                Weight = item.Weight,
                            };
            count = recommend.Count();

            //if (!string.IsNullOrWhiteSpace(ShopId.ToString()))
            //{
            //    recommend = recommend.Where(p => p.ShopId == ShopId);
            //}


            recommend = recommend.OrderBy(p => p.Id);
            recommend = recommend.Skip((page - 1) * limit).Take(limit);
            return recommend.ToList();
        }
       
        public List<RecommendModel> GetRecommedTest(int limit, int page, int shopId, int recId, out int count)
        {
            var recommend = from item in db.Recommends.Where(p => true)
                            select new RecommendModel
                            {
                                Id = item.Id,
                                //RecId = item.RecId,
                                ShopId = item.ShopId,
                                Weight = item.Weight,
                            };
            count = recommend.Count();
            //if (!string.IsNullOrWhiteSpace(ShopId.ToString()))
            //{
            //    recommend = recommend.Where(p => p.ShopId == ShopId);
            //}
            if (shopId != 0)
            {
                recommend = recommend.Where(p => p.ShopId == shopId);
            }
            if (recId != 0)
            {
                recommend = recommend.Where(p => p.RecId == recId);
            }
            //if (shopId > 0)
            //{
            //    recommend = recommend.Where(p => p.ShopId == shopId);
            //}
            recommend = recommend.OrderBy(p => p.Id);
            recommend = recommend.Skip((page - 1) * limit).Take(limit);
            return recommend.ToList();
        }
        /// <summary>
        /// 显示推荐位置方法
        /// </summary>
        /// <param name="site"></param>
        /// <returns></returns>
        public List<RecPositionModel> RecPosition(int limit, int page, string site, out int count)
        {
            var recPosition = from item in db.RecPositions.Where(p => true)
                              select new RecPositionModel
                              {
                                  Id = item.Id,
                                  Site = item.Site
                              };
            count = recPosition.Count();
            if (!string.IsNullOrWhiteSpace(site))
            {
                recPosition = recPosition.Where(p => p.Site == site);
            }
            recPosition = recPosition.OrderBy(p => p.Id);
            recPosition = recPosition.Skip((page - 1) * limit).Take(limit);
            return recPosition.ToList();
        }
        public int AddRecPositions(RecPosition rec)
        {
            db.RecPositions.Add(rec);
            int n = db.SaveChanges();
            return n;
        }
        /// <summary>
        /// 删除推荐位置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelPosition(int id)
        {
            var shop = db.RecPositions.Where(p => p.Id == id).ToList();
            //查找推荐表id 判断位置表下是否有这个商品
            var reccommend = db.Recommends.Where(p => p.RecId == id).ToList();
            //var  reccommend1 = reccommend.SingleOrDefault();
            if (reccommend.Count() > 0)
            {
                return 0;
            }
            else
            {
                var shop1 = shop[0];
                db.RecPositions.Remove(shop1);
                var res = db.SaveChanges();
                return res;
            }


        }
        public int DelRecommend(int id)
        {
            var rec = db.Recommends.Where(p => p.Id == id).ToList();
            if (rec.Count() > 0)
            {
                var rec1 = rec[0];
                db.Recommends.Remove(rec1);
                var res = db.SaveChanges();
                return res;

            }
            else
            {
                return rec.Count();
            }
        }
    }
}
